package cn.hxzy.kaoshi.mapper;


import cn.hxzy.kaoshi.entity.SysSimulatedExam;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;


/**
 * <p>
 * 模拟考试 Mapper 接口
 * </p>
 *
 * @author tyqw
 * @since 2024-12-13
 */
public interface SysSimulatedExamMapper extends BaseMapper<SysSimulatedExam> {
    // 查询10道单选题
    @Select("SELECT * FROM sys_subject WHERE subject_type = 2 " +
            "AND subject_major=#{subjectMajor} and subject_lv=#{subjectLv}  AND isdelete!=1 " +
            "AND sys_subject.subject_answer NOT REGEXP '[\\\\u4e00-\\\\u9fa5]'  " +
            "ORDER BY RAND() LIMIT 10")
    List<Map> getRandomSingleChoiceQuestions(Map<String,Object> map);

    // 查询5道多选题
    @Select("SELECT * FROM sys_subject WHERE subject_type = 1" +
            " AND subject_major=#{subjectMajor}  and subject_lv=#{subjectLv} AND isdelete!=1 " +
            "AND sys_subject.subject_answer NOT REGEXP '[\\\\u4e00-\\\\u9fa5]'  " +
            "ORDER BY RAND() LIMIT 5")
    List<Map> getRandomMultipleChoiceQuestions(Map<String,Object> map);

    // 查询5道判断题
    @Select("SELECT * FROM sys_subject WHERE subject_type = 0" +
            " AND subject_major=#{subjectMajor} and subject_lv=#{subjectLv} AND isdelete!=1 " +
            "AND sys_subject.subject_answer NOT REGEXP '[\\\\u4e00-\\\\u9fa5]'  " +
            "ORDER BY RAND() LIMIT 5")
    List<Map> getRandomTrueFalseQuestions(Map<String,Object> map);
    //根据用户id查询模拟考试记录
    @Select("select * from sys_simulated_exam where sys_simulated_exam.user_id=#{userId}")
    List<SysSimulatedExam> findByUserId(Long userId);
    /**
     * 根据考试ID查询所有问题详情
     * 此方法通过MyBatis的@Select注解执行一个预编译的SQL查询语句，以获取与特定考试相关的所有问题信息
     * 它从sys_questiones和sys_subject表中联接查询，返回包含问题详细信息的列表，如问题ID、问题名称、选项和答案等
     *
     * @param examId 考试ID，用于指定查询哪个考试的问题信息
     * @return 返回一个包含问题详细信息的List，每个问题的信息以Map形式存储，键为字段名，值为对应的字段值
     */
    @Select("SELECT sys_subject.subject_id,sys_subject.subject_name,sys_subject.option_a,sys_subject.option_b,sys_subject.option_c,sys_subject.option_d,sys_subject.option_e,sys_subject.option_f,sys_subject.subject_answer,sys_subject.subject_type,sys_questiones.questiones_examination,sys_questiones.`questiones_score` " +
            "FROM sys_questiones INNER JOIN sys_subject ON sys_questiones.questiones_subject = sys_subject.subject_id " +
            "WHERE sys_questiones.questiones_examination = #{examId}")
    List<Map<String, Object>> findAllQuestions(@Param("examId") Integer examId);
    /**
     * 根据考试ID查询考试信息和相关问题
     *
     * @param id 考试ID
     * @return 包含考试信息和问题的Map对象
     */
    @Select("SELECT * FROM sys_examination, sys_questiones " +
            "WHERE sys_examination.examination_id = sys_questiones.questiones_examination " +
            "AND sys_questiones.questiones_examination = #{id} LIMIT 1")
    Map<String, Object> findexaminationById(Integer id);

    @Insert("INSERT INTO sys_exam_results (content, scor, duration, student_id, submit_time,start_exam_time,examination_id,exam_id) " +
                "VALUES (#{content}, #{scor}, #{duration}, #{student_id}, #{submit_time},#{start_exam_time},#{examination_id},#{exam_id})")
    int insertSimulatedExam(Map<String, Object> examData);
    @MapKey("user_id")
    List<Map> findAllExamResults2(Long userId);
    }
